﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCWeb.Models;
using System.Web.Security;

namespace MVCWeb.Helpers
{
    public class VisitorCountHelper
    {
        public static string CommunityVisitorCount()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                try
                {
                    HOACRMDataContext dc = new HOACRMDataContext();

                    MembershipUser membershipUser = Membership.GetUser();
                    Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
                    MVCWeb.Models.User user = dc.Users.Where<MVCWeb.Models.User>(u => u.MembershipId == membershipId).First<MVCWeb.Models.User>();
                    Community community = dc.Communities.Single<Community>(c => c.UserId == user.UserId || c.ManagerId == user.UserId);
                    return community.VisitorCount.HasValue == true ? "Community Visitor: " + community.VisitorCount.Value : "Community Visistor: 1";
                }
                catch 
                {
                    return ""; 
                }
            }
            return ""; 
        }

        public static void IncreaseCommunityVisitorCount(string userName)
        {
            if (!HttpContext.Current.User.IsInRole("Administrators"))
            {
                try
                {
                    HOACRMDataContext dc = new HOACRMDataContext();

                    MembershipUser membershipUser = Membership.GetUser(userName);
                    Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
                    MVCWeb.Models.User user = dc.Users.Where<MVCWeb.Models.User>(u => u.MembershipId == membershipId).First<MVCWeb.Models.User>();
                    Community community = dc.Communities.Single<Community>(c => c.UserId == user.UserId || c.ManagerId == user.UserId);
                    if (community.VisitorCount.HasValue)
                        community.VisitorCount = community.VisitorCount + 1;
                    else
                        community.VisitorCount = 1;

                    dc.SubmitChanges();
                }
                catch { }
            }
        }

        public static void IncreaseSiteVisitorCount()
        {
            HOACRMDataContext dc = new HOACRMDataContext();
            int companyId = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CompanyId"]);
            Company company = dc.Companies.Single<Company>(c => c.CompanyId == companyId);
            if (company.VisitorCount.HasValue)
                company.VisitorCount = company.VisitorCount + 1;
            else
                company.VisitorCount = 1;

            dc.SubmitChanges();
        }

        public static string SiteVisitorCount()
        {
            HOACRMDataContext dc = new HOACRMDataContext();
            int companyId = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CompanyId"]);
            Company company = dc.Companies.Single<Company>(c => c.CompanyId == companyId);
            if (company.VisitorCount.HasValue)
                return "Site Visitor: " + company.VisitorCount.Value;
            else
                return "Site Visitor: " + 1;
        }
    }
}
